package com.ksxy.afingerprint.ui.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import android.view.View
import com.ksxy.afingerprint.R
import com.ksxy.afingerprint.utils.EnvUtils
import kotlinx.android.synthetic.main.activity_main.*

/**
 *  author : chenyinying
 *  e-mail : me@korion.cn
 *  time   : 2018/07/06
 *  desc   :
 *  version: 1.0
 */
class MainActivity: AppCompatActivity(), View.OnClickListener {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
        checkDeviceSupportFingerPrintAuth()
    }

    private fun checkDeviceSupportFingerPrintAuth() {
        val result = EnvUtils.checkSupportFingerPrintAuth(this)
        if(result != EnvUtils.CODE_FINGER_PRINT_AUTH_OK){
            disableView()
            AlertDialog.Builder(this)
                    .setTitle(getString(R.string.fingerprint_disable))
                    .setMessage(EnvUtils.getCheckResultDesc(result))
                    .setPositiveButton(getString(R.string.confirm)){
                        dialog, which -> dialog.dismiss()
                    }
                    .show()
        }
    }

    private fun disableView(){
        btn_createKey.isEnabled = false
        btn_listKeys.isEnabled = false
        btn_encrypt_or_decrypt.isEnabled = false
    }

    private fun initView() {
        btn_listKeys.setOnClickListener(this)
        btn_createKey.setOnClickListener(this)
        btn_encrypt_or_decrypt.setOnClickListener(this)
    }


    private fun launchActivity(clazz: Class<out Activity>){
        val intent = Intent(this, clazz)
        startActivity(intent)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.main_menu, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when(item!!.itemId){
            R.id.main_menu_about -> {
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onClick(v: View?) {
        when(v!!.id){
            R.id.btn_createKey -> {
                launchActivity(CreateKeyActivity::class.java)
            }
            R.id.btn_listKeys -> {
                launchActivity(ListKeysActivity::class.java)
            }
            R.id.btn_encrypt_or_decrypt -> {
                launchActivity(EncryptOrDecryptActivity::class.java)
            }

        }
    }


}